﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Data.Objects;



namespace BusinessObject
{
    /// <summary>
    /// Implementação da a interface para CRUD genérico.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        private ObjectContext context;

        private IObjectSet<T> objectSet;

        public GenericRepository(ObjectContext context)
        {
            this.context = context;
            objectSet = this.context.CreateObjectSet<T>();
        }

       
        /// <summary>
        /// Cria a entidade.
        /// </summary>
        /// <param name="entity"></param>
        public void Save(T entity) 
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            objectSet.AddObject(entity);
            context.SaveChanges();
        }

        /// <summary>
        /// Edita da entidade.
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Update(T entity)
        {
            


        }

        /// <summary>
        /// Deleta a entidade.
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Delete(T entity)
        {

        }

        /// <summary>
        /// Retorna a entidade de acordo com o id passado.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T GetById(int id)
        {
            ///TODO: Implementar o get byID
            return objectSet.First();
        }

        /// <summary>
        /// Retorna todos os objetos da entidade.
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> GetAll()
        {
            return objectSet.ToList();
        }

        
    }
}
